﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication5.Controller;
using WebApplication5.Model;

namespace WebApplication5.Pages
{
    public partial class task : System.Web.UI.Page
    {
        protected taskCtr tCtr = new taskCtr();
        protected userstoriesCtr uCtr = new userstoriesCtr();
        protected void Page_Load(object sender, EventArgs e)
        {
            Site1 s = Page.Master as Site1;
            s.SelectedMenu = "task";


            if (!IsPostBack)
            {
                refresh();
            }
                //                        List<Model.Task> ua = tCtr.GetAllTasks();
                //var datasource = from u in ua
                //                 select new
                //                 {
                //                     u.name,
                //                     u.id,
                //                     u.status,
                //                     u.type,
                //                     u.Userstories,
                //                     u.userstoriesId,
                //                     DisplayField = String.Format("{0} ({1})", u.name, u.status)
                //                 };

                //DropDownListEdit.DataSource = datasource;
                //DropDownListEdit.DataValueField = "id";
                //DropDownListEdit.DataTextField = "DisplayField";
                //DropDownListEdit.DataBind();


                //DropDownListDelete.DataSource = datasource;
                //DropDownListDelete.DataValueField = "id";
                //DropDownListDelete.DataTextField = "DisplayField";
                //DropDownListDelete.DataBind();
            
        }

        protected void refresh()
        {
            List<Model.Task> us = tCtr.GetAllTasks();
            var datasource = from u in us
                             select new
                             {
                                 u.id,
                                 u.name,
                                 u.status,
                                 u.type,
                                 u.Userstory,
                                 u.userstoryId,
                                 DisplayField = String.Format("{0} ({1})", u.name, u.status)
                             };

            DropDownListEdit.DataSource = datasource;
            DropDownListEdit.DataValueField = "id";
            DropDownListEdit.DataTextField = "DisplayField";
            DropDownListEdit.DataBind();


            DropDownListDelete.DataSource = datasource;
            DropDownListDelete.DataValueField = "id";
            DropDownListDelete.DataTextField = "DisplayField";
            DropDownListDelete.DataBind();



            List<Model.Userstory> user = uCtr.GetAllUserstories();
            var datasourceUser = from use in user
                             select new
                             {
                                 use.id,
                                 use.name,
                                 use.iWantTo,
                                 use.actual,
                                 use.asA,
                                 use.Estimate,
                                 use.priority,
                                 use.Project,
                                 use.projectId,
                                 use.soThat,
                                 use.Task,
                                 
                                 
                                 DisplayField = String.Format("{0} ({1})", use.name, use.priority)
                             };

            DropDownListEditUserstories.DataSource = datasourceUser;
            DropDownListEditUserstories.DataValueField = "id";
            DropDownListEditUserstories.DataTextField = "DisplayField";
            DropDownListEditUserstories.DataBind();


            DropDownListAddUserstory.DataSource = datasourceUser;
            DropDownListAddUserstory.DataValueField = "id";
            DropDownListAddUserstory.DataTextField = "DisplayField";
            DropDownListAddUserstory.DataBind();

        }


        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Task u = new Task();
                u.id = int.Parse(DropDownListEdit.SelectedValue);
                u.name = nameTbe.Text;
                u.status = statusTbe.Text;
                u.type = typeTbe.Text;
                u.Userstory = null;
                u.userstoryId = int.Parse(DropDownListEditUserstories.SelectedValue);

                tCtr.editTask(u);

                Response.Redirect(Request.RawUrl);

                refresh();

              




            }


            catch (Exception ex)
            {
                Shared.alert(ex.Message, this);
            }

            Shared.reloadCurrentTab(this);
            Response.Redirect(Request.RawUrl);

        }




        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            int id = int.Parse(DropDownListDelete.SelectedValue);

            tCtr.deleteTask(id);

            Shared.reloadCurrentTab(this);

            refresh();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {

                Task u = new Task();

                u.name = nameTb.Text;
                u.status = statusTb.Text;
                u.type = typeTb.Text;
                u.Userstory = null;
                u.userstoryId = int.Parse(DropDownListAddUserstory.SelectedValue);

                tCtr.AddTask(u);

            
            statusTb.Text = "";
            typeTb.Text = "";
            nameTb.Text = "";

            refresh();

        }

        protected void ButtonGetTask_Click(object sender, EventArgs e)
        {


            int id = int.Parse(DropDownListEdit.SelectedValue);

            var t = tCtr.GetTask(id);

            
            nameTbe.Text = t.name;
            statusTbe.Text = t.status;
            typeTbe.Text = t.type;


        }
    
        
    
        
        
    }
}